﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZDBase
{
    public partial class ZDEditor : Form
    {
        OpenFileDialog _currentFileName;
        Dictionary<string, OpenFileDialog> _dlgContainer;

        public ZDEditor()
        {
            InitializeComponent();
            InitApp();
        }

        private void InitApp()
        {
            _dlgContainer = new Dictionary<string, OpenFileDialog>(0);
            treeViewFiles.MouseDoubleClick += new MouseEventHandler(treeViewFiles_MouseDoubleClick);
            WorkTab.OnClose += new TabCtlEx.OnHeaderCloseDelegate(WorkTab_OnClose);
            WorkTab.ItemSize = new Size(180, 24);
            WorkTab.ConfirmOnClose = false;
            WorkTab.Selected += new TabControlEventHandler(WorkTab_Selected);
        }

        void WorkTab_Selected(object sender, TabControlEventArgs e)
        {
            if (e.TabPage!=null)
            {
                _currentFileName=_dlgContainer[e.TabPage.Text.Trim()];
            }
           
        }

        void WorkTab_OnClose(object sender, CloseEventArgs e)
        {
            TabPageEx tab = (TabPageEx)WorkTab.TabPages[e.TabIndex];
            if (((FileTab)tab.Controls[0]).mod)
            {
                OpenFileDialog dlg = (OpenFileDialog)((FileTab)tab.Controls[0])._dlg;
                SaveFileDialog sv = new SaveFileDialog();
                sv.FileName = dlg.FileName;
               ((FileTab)tab.Controls[0]).SaveFile(sv.OpenFile());
                
            }
            _dlgContainer.Remove(tab.Text.Trim());
            WorkTab.TabPages.Remove(WorkTab.TabPages[e.TabIndex]);

        }

        private void открытьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult res = openFile.ShowDialog();
            if (res == DialogResult.OK)
            {
                
                if (!_dlgContainer.ContainsKey(openFile.SafeFileName))
                {

                    if (_currentFileName == null)
                    {
                        _currentFileName = openFile;
                    }
                    TreeNode _treeNode = new TreeNode(openFile.SafeFileName);
                    treeViewFiles.Nodes[0].Nodes.Add(_treeNode);
                    _treeNode.Tag = openFile;
                    _dlgContainer.Add(openFile.SafeFileName, openFile);
                    treeViewFiles.Nodes[0].ExpandAll();
                }
            }
        }

        private void treeViewFiles_AfterSelect(object sender, TreeViewEventArgs e)
        {
           
        }

        void treeViewFiles_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            TreeNode _treeNode = (TreeNode)((TreeView)sender).SelectedNode;

            if (_treeNode.Tag != null && typeof(OpenFileDialog) == _treeNode.Tag.GetType() && e.Button == System.Windows.Forms.MouseButtons.Left && e.Clicks==2)
            {

                if (!WorkTab.TabPages.ContainsKey(_treeNode.Text))
                {

                    TabPageEx page = new TabPageEx();
                    page.Text = _treeNode.Text.Trim();
                    page.Name = _treeNode.Text;
                    WorkTab.TabPages.Add(page);
                    
                  //  TabPage page = WorkTab.TabPages[_treeNode.Text];
                    page.Controls.Add(new FileTab() { Dock = DockStyle.Fill, _dlg = (OpenFileDialog)_treeNode.Tag });
                    
                    //MenuItem CloseMenu=new MenuItem("Close");
                    //CloseMenu.Click+=new EventHandler(CloseMenu_Click);
                    ////CloseMenu.Tag = page;
                    //WorkTab.ContextMenu = new ContextMenu();
                    //WorkTab.ContextMenu.MenuItems.Add(CloseMenu);
                   
                }

            }
        }

        void CloseMenu_Click(object sender, EventArgs e)
        {
            MenuItem CloseMenuItm = (MenuItem)sender;
            WorkTab.TabPages.Remove(WorkTab.SelectedTab);
        }
    }
}
